
import { getFullDefinePageById, saveDefinePageBrowsingHistory } from '@/apis/index'
export default {
  data() {
    return {
      pageId: '', // 自定义页面ID
      pageName: '',
      brownHistoryId: '',
      skuCodes: [], // SKUP商品查询前需先查询所有sku编码
      loadPageState: false, // 自定义页数据是否加载完成
      loadText: '加载中...'
    }
  },
  methods: {
    /**
     * params {boolen}  isSku 是否sku页面处理
     * params {function}  callback  回调函数
    */
    async getCustomPageContent(isSku, callback) {
      return await getFullDefinePageById(this.pageId).then(res => {
        const data = res.data || ''
        this.pageName = data ? data.name || '' : ''
        if (this.brownHistoryId !== this.pageId && this.$route.name === 'CustomPage') {
          this.setBrowsingHistory(data)
        }
        if (isSku) {
          this.getProductSkuCode(data)
        }
        this.loadPageState = true
        callback && callback(data)
      }).catch(() => {
        this.loadPageState = true
        this.loadText = '数据获取超时，请稍后重试'
      })
    },
    // 保存浏览记录
    setBrowsingHistory({ id, name }) {
      if (!id) return
      const params = { definePageId: id, definePageName: name }
      saveDefinePageBrowsingHistory(params).then(res => {
        this.brownHistoryId = id
      })
    },
    getProductSkuCode(data) {
      if (!data) return
      const pageType = data.type || '' // 页面类型：product 商品 ， combination 组合
      if (pageType !== 'product') {
        this.loadText = '非自定义页面商品展示页，不可访问'
        return
      } else {
        // 商品展示类型
        const floorList = data ? data.v2FloorList || [] : []
        if (floorList.length > 0 && floorList[0].showType === 'SPU') {
          // SPU展示形式，查询gcenter数据接口
          const tmpObj = floorList[0]
          if (tmpObj.productDetailList && tmpObj.productDetailList.length > 0) {
            this.skuCodes = tmpObj.productDetailList.map(v => v.skuCode)
          }
          this.loadPageState = true
          this.$nextTick(() => {
            this.broadcast('refresh', 'refresh')
          })
          // this.$router.replace({ path: `/customPage/${this.pageId}/spu/list` })
        }
      }
    }
  }
}
